﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class FindPass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string userName = Request.Form["userName"].Trim();
            DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("userdata"));
            DirectoryInfo[] folders = TheFolder.GetDirectories(userName);
            if (folders.Length == 0)
            {
                Response.Write("<script type='text/javascript'>alert('不存在这个用户名!');</script>");
                return;
            }
            XmlDocument doc = new XmlDocument();
            try
            {
                string userxml = Server.MapPath("userdata/" + userName + "/user.xml");
                doc.Load(userxml);
                string em = doc.SelectSingleNode("/info/em").InnerText;
                string ps = doc.SelectSingleNode("/info/ps").InnerText;
                //var isup = doc.SelectSingleNode("/info/isup");
                string sjs = getStr(20);
                XmlNodeList nodehave = doc.GetElementsByTagName("isup");
                if (nodehave == null || nodehave.Count <= 0)
                {
                    var root = doc.DocumentElement;//取到根结点
                    XmlNode newNode = doc.CreateNode("element", "isup", "");
                    newNode.InnerText = "true";
                    root.AppendChild(newNode);
                    XmlNode newNode2 = doc.CreateNode("element", "sjs", "");
                    newNode2.InnerText = sjs;
                    root.AppendChild(newNode2);
                }
                else
                {
                    doc.SelectSingleNode("/info/isup").InnerText = "true";
                    doc.SelectSingleNode("/info/sjs").InnerText = sjs;
                }
                   
                doc.Save(userxml);
                if (SendPassEmail(em, ps, userName, sjs))
                {
                    Response.Write("<script type='text/javascript'>alert('邮件成功发送，请注意查收!');</script>");
                }
                else
                {
                    Response.Write("<script type='text/javascript'>alert('邮件发送失败!');</script>");
                }
            }
            catch(Exception ex)
            {
                Response.Write("<script type='text/javascript'>alert('读取用户信息失败!');</script>");
                return;
            }

        }
    }
    public string getStr(int n)//b：是否有复杂字符，n：生成的字符串长度
    {

        string str = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        StringBuilder SB = new StringBuilder();
        Random rd = new Random();
        for (int i = 0; i < n; i++)
        {
            SB.Append(str.Substring(rd.Next(0, str.Length), 1));
        }
        return SB.ToString();

    }
    private bool SendPassEmail(string em, string ps,string un,string s)
    {
        string myMail = "459281182@qq.com";//"wrsfds23@163.com";
        string myPass = "kyrfmpgelpnfcbbf";//"rew24uy1";
        string webTitle = "导航网";
        Encoding encode = Encoding.UTF8;
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.From = new MailAddress(myMail, webTitle + "管理中心", encode);
        msg.To.Add(new MailAddress(em, "尊敬的用户", encode));

        msg.Subject = string.Format(webTitle + "找回密码", ps);
        msg.SubjectEncoding = encode;
        StringBuilder sb = new StringBuilder();
        sb.Append("尊敬的用户：<br/>");
        sb.Append("您好！你于");
        sb.Append(DateTime.Now.ToLongTimeString());
        sb.Append("通过<a href='rpass.aspx?u=" + un + "&s=" + s + "'>点击链接</a>修改密码,修改后链接失效，不能再次修改。<br />");
        sb.AppendFormat("<br /><br />"+ webTitle + ":&nbsp;<a href='{0}' />{0}</a>", "http://www.yanglihao.net");
        msg.Body = sb.ToString();
        msg.BodyEncoding = encode;
        msg.IsBodyHtml = true;
        try
        {
            SmtpClient smtp = new SmtpClient("smtp.qq.com");// new SmtpClient("smtp.163.com", 25);
            smtp.Credentials = new NetworkCredential(myMail, myPass);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(msg);
        }
        catch (Exception ex)
        {
            Response.Write("<script type='text/javascript'>alert('读取用户信息失败:" + ex.Message + "');</script>");
            return false;
        }
        finally
        {
            msg.Dispose();
        }
        return true;
    }
}